﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Manage
{
    public class sysModule
    {
        public static Hashtable get(string moduleid)
        {
            Hashtable keys = new Hashtable();
            keys["moduleid"] = moduleid;
            return ERMapHelper.getmodel("sysmodule", keys);
        }

        public static Hashtable getByUrl(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from sysmodule ");
            sb.Append(" where urladdress like @url ");

            Hashtable keys = new Hashtable();
            keys["url"] = url + "%";

            return ERMapHelper.getCustomModel(sb.ToString(), keys);
        }

        public static DataSet list()
        {
            return ERMapHelper.getCustomData("select * from sysmodule order by systemid,case when isnull(parentid,'') = '' then moduleid else parentid end,parentid,showxh", null);
        }

        public static void del(string moduleid)
        {
            Hashtable sysmodule = new Hashtable();
            sysmodule["moduleid"] = moduleid;
            ERMapHelper.destroy("sysmodule", sysmodule);
        }

        public static DataSet getTopModule(string systemid)
        {
            string sql = "select * from sysmodule where systemid = @systemid and isnull(parentid,'')=''";
            Hashtable param = new Hashtable();
            param.Add("systemid", systemid);
            return ERMapHelper.getCustomData(sql, param);
        }

        public static void add(Hashtable sysmodule)
        {
            sysmodule["moduleid"] = Commons.StringUtil.GetID_20();
            switch (Commons.StrUtil.FormatValue(sysmodule["systemid"]))
            {
                case "manager":
                    sysmodule["usertype"] = "0";
                    break;
                case "group":
                    sysmodule["usertype"] = "2";
                    break;
                case "webuser":
                    sysmodule["usertype"] = "4";
                    break;
                default:
                    break;
            }
            ERMapHelper.setmodel("sysmodule", "moduleid", sysmodule);
        }

        public static void edit(Hashtable sysmodule)
        {
            switch (Commons.StrUtil.FormatValue(sysmodule["systemid"]))
            {
                case "manager":
                    sysmodule["usertype"] = "0";
                    break;
                case "group":
                    sysmodule["usertype"] = "2";
                    break;
                case "webuser":
                    sysmodule["usertype"] = "4";
                    break;
                default:
                    break;
            }
            ERMapHelper.setmodel("sysmodule", "moduleid", sysmodule);
        }
    }
}
